﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using SHomeWorkshop.LunarTools;
using System.Windows.Input;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class CommentSizeHandlerAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示Comment改变宽度的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入Comment的“TextBlock”主子元素。</param>
        public CommentSizeHandlerAdorner(UIElement adornedElement, Comment masterComment)
            : base(adornedElement)
        {
            this.masterComment = masterComment;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.Cursor = Cursors.SizeWE;
            this.ToolTip = "左键拖动手动设置宽度（带自动折行）,\r\n右键单击自动调整宽度（不自动折行）";

            this.MouseLeftButtonDown += new MouseButtonEventHandler(CommentSizeHandlerAdorner_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(CommentSizeHandlerAdorner_MouseLeftButtonUp);

            this.MouseRightButtonDown += new MouseButtonEventHandler(CommentSizeHandlerAdorner_MouseRightButtonDown);
        }

        void CommentSizeHandlerAdorner_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            masterComment.Editor.isResizeCommentWidth = false;

            if (double.IsNaN(masterComment.TextBlockWidth))
            {
                masterComment.MainTextPanel.Width = double.NaN;
            }
            else
            {
                masterComment.MainTextPanel.Width = masterComment.MainTextPanel.ActualWidth;
            }
        }

        void CommentSizeHandlerAdorner_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Globals.MainWindow.isResizeCommentWidth = false;

            if (masterComment == null) return;

            //直接设置为double.NaN.
            ModifingItem mi = new ModifingItem()
            {
                ModifingName = "自动设置备注文本块宽度",
            };

            Action act = new Action(masterComment.ID, -1, masterComment.GetType().Name, "TextBlockWidth",
                masterComment.TextBlockWidth.ToString(), double.NaN.ToString());

            masterComment.TextBlockWidth = double.NaN;
            masterComment.RefreshConnectLine();

            if (masterComment.IsSummary)
            {
                mi.IsNeedRefreshTitleAndLinesLocation = true;
                masterComment.Editor.RefreshTitleAndLinesLocation();
            }
            else
            {
                mi.IsNeedRefreshTitleAndLinesLocation = false;
            }

            mi.Add(act);

            masterComment.Editor.Do(mi);
            masterComment.Editor.IsModified = true;

            e.Handled = true;
        }

        void CommentSizeHandlerAdorner_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (masterComment == null) return;

            masterComment.Editor.isResizeCommentWidth = true;

            foreach (UIElement ue in masterComment.MainTextPanel.Children)
            {
                TextBlock tb = ue as TextBlock;
                if (tb != null)
                {
                    if (tb.TextWrapping != TextWrapping.Wrap)
                    {
                        tb.TextWrapping = TextWrapping.Wrap;
                    }
                }
            }

            masterComment.MainTextPanel.MaxWidth = double.MaxValue;

            e.Handled = true;
        }

        private Comment masterComment;

        public Comment mMasterComment
        {
            get { return masterComment; }
        }

        //private static LinearGradientBrush handlerFillBrush;

        //static CommentSizeHandlerAdorner()
        //{
        //    handlerFillBrush = new LinearGradientBrush() { StartPoint = new Point(0, 0), EndPoint = new Point(1, 0) };

        //    handlerFillBrush.GradientStops.Add(new GradientStop() { Offset = 0.1, Color = Colors.Transparent });
        //    handlerFillBrush.GradientStops.Add(new GradientStop() { Offset = 0.2, Color = Colors.Blue });
        //    handlerFillBrush.GradientStops.Add(new GradientStop() { Offset = 0.5, Color = Colors.Blue });
        //    handlerFillBrush.GradientStops.Add(new GradientStop() { Offset = 0.8, Color = Colors.Blue });
        //    handlerFillBrush.GradientStops.Add(new GradientStop() { Offset = 0.9, Color = Colors.Transparent });
        //}

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (masterComment == null) return;

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            Pen linePen = null;

            if (this.masterComment.Editor != null && masterComment.IsSelected)
            {
                Brush borderColor = masterComment.BorderColor;

                linePen = new Pen(Brushes.Blue, 1);

                Point newTopLeft = new Point(adornedElementRect.TopLeft.X - 18, adornedElementRect.TopLeft.Y - 18);
                Point ptTmp = new Point(adornedElementRect.BottomRight.X + 18, adornedElementRect.BottomRight.Y + 18);
                //Rect selectedRect = new Rect(newTopLeft, ptTmp);


                //画右侧宽度调节柄。这个调节柄应覆盖在Selected框的右连线上。

                if (masterComment.MasterTitle == null) return;

                if (masterComment.IsSummary)
                {
                    drawingContext.DrawRectangle(Brushes.Blue, linePen,
                            new Rect(adornedElementRect.Right,
                                adornedElementRect.Top - 2,
                                4,
                                adornedElementRect.Height + 4
                                ));
                }
                else
                {
                    Rect outBorderRect = new Rect(adornedElementRect.Left - 10, adornedElementRect.Top - 10,
                        adornedElementRect.Width + 20, adornedElementRect.Height + 20);
                    drawingContext.DrawRectangle(Brushes.Blue, linePen,
                        new Rect(outBorderRect.Right + 6, outBorderRect.Top - 10, 4, outBorderRect.Height + 20));
                }
            }
        }
    }
}
